import * as ActionTypes from '../../constants/ActionType';
import * as APIS from '../../constants/APIS';
import { fetchPOST, fetchGET } from '../../utils/index';
var path = require('path');

const {
    SET_ADMIN_STATE,
    GET_ARTICLE_ADMIN
     } = ActionTypes;

export const setState = (value) => { return dispatch => dispatch({ type: SET_ADMIN_STATE, payload: value }) };
export const getArticle = () => {
    return dispatch => {
        fetchGET(APIS.IS_GET_ARTICLES, {})
            .then((res) => {
                dispatch({ type: GET_ARTICLE_ADMIN, payload: res.data });
            })
    }
}

export const savePendingArticle = (obj) => {
    return dispatch => {
        if (obj._id) {
            fetchPOST(APIS.IS_UPDATE_ARTICLES, obj)
                .then((res) => {
                    dispatch(getArticle());
                })
        } else {
            fetchPOST(APIS.IS_ADD_ARTICLES, obj)
                .then((res) => {
                    dispatch(getArticle());
                })
        };
        dispatch(setState({ newArticleObj: undefined }))
    }
}

export const deleteArticle = (id) => {
    return dispatch => {
        fetchGET(APIS.IS_DELETE_ARTICLES, { _id: id })
            .then((res) => {
                dispatch(getArticle());
            })
    }
}

